package com.secondhand.project.controller;

import com.secondhand.common.annotation.Log;
import com.secondhand.common.core.controller.BaseController;
import com.secondhand.common.core.domain.AjaxResult;
import com.secondhand.common.core.page.TableDataInfo;
import com.secondhand.common.enums.BusinessType;
import com.secondhand.common.utils.poi.ExcelUtil;
import com.secondhand.project.domain.Collect;
import com.secondhand.project.service.ICollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/project/collect")
public class CollectController extends BaseController {
    @Autowired
    private ICollectService collectService;

    @PreAuthorize("@ss.hasPermi('project:collect:list')")
    @GetMapping("/list")
    public TableDataInfo list(Collect collect) {
        startPage();
        List<Collect> list = collectService.selectCollectList(collect);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('project:collect:export')")
    @Log(title = "收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Collect collect) {
        List<Collect> list = collectService.selectCollectList(collect);
        ExcelUtil<Collect> util = new ExcelUtil<Collect>(Collect.class);
        util.exportExcel(response, list, "收藏数据");
    }

    @PreAuthorize("@ss.hasPermi('project:collect:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(collectService.selectCollectById(id));
    }

    @PreAuthorize("@ss.hasPermi('project:collect:add')")
    @Log(title = "收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Collect collect) {
        return collectService.insertCollect(collect) > 0 ? success() : error("已收藏");
    }

    @PreAuthorize("@ss.hasPermi('project:collect:edit')")
    @Log(title = "收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Collect collect) {
        return toAjax(collectService.updateCollect(collect));
    }

    @PreAuthorize("@ss.hasPermi('project:collect:remove')")
    @Log(title = "收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(collectService.deleteCollectByIds(ids));
    }
}
